﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.IO;
using System.Linq;
using UnityEngine;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class Color32Processor : GenericDataProcessor<Color32>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Color32";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "color32",
                    "unityengine.color32"
                };
            }

            public override Color32 Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return Color.white;
                }
                string[] splitedValue = value.Split(',');
                return new Color32(byte.Parse(splitedValue[0]), byte.Parse(splitedValue[1]), byte.Parse(splitedValue[2]), byte.Parse(splitedValue[3]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                Color32 color32 = Parse(value);
                binaryWriter.Write(color32.r);
                binaryWriter.Write(color32.g);
                binaryWriter.Write(color32.b);
                binaryWriter.Write(color32.a);
            }
        }
        private sealed class Color32ArrayProcessor : GenericDataArrayProcessor<Color32>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Color32[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "color32[]",
                    "unityengine.color32[]",
                };
            }

            public override Color32 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return Color.white;
                }
                string[] splitedValue = value.Split(',');
                return new Color32(byte.Parse(splitedValue[0]), byte.Parse(splitedValue[1]), byte.Parse(splitedValue[2]), byte.Parse(splitedValue[3]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    Color32 color32 = array[i];
                    binaryWriter.Write(color32.r);
                    binaryWriter.Write(color32.g);
                    binaryWriter.Write(color32.b);
                    binaryWriter.Write(color32.a);
                }
            }
        }
        private sealed class Color32ListProcessor : GenericDataListProcessor<Color32>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<Color32>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<color32>",
                    "list<unityengine.color32>",
                    "system.collections.generic.list<color32>",
                    "system.collections.generic.list<unityengine.color32>",
                };
            }

            public override Color32 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return Color.white;
                }
                string[] splitedValue = value.Split(',');
                return new Color32(byte.Parse(splitedValue[0]), byte.Parse(splitedValue[1]), byte.Parse(splitedValue[2]), byte.Parse(splitedValue[3]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    Color32 color32 = array[i];
                    binaryWriter.Write(color32.r);
                    binaryWriter.Write(color32.g);
                    binaryWriter.Write(color32.b);
                    binaryWriter.Write(color32.a);
                }
            }
        }
        private sealed class Color32DictionaryProcessor : GenericDataDictionaryProcessor<Color32>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,Color32>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,color32>",
                    "dictionary<string,unityengine.color32>",
                    "dictionary<system.string,color32>",
                    "dictionary<system.string,unityengine.color32>",
                    "system.collections.generic.dictionary<string,color32>",
                    "system.collections.generic.dictionary<string,unityengine.color32>",
                    "system.collections.generic.dictionary<system.string,color32>",
                    "system.collections.generic.dictionary<system.string,unityengine.color32>",
                };
            }

            public override Color32 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return Color.white;
                }
                string[] splitedValue = value.Split(',');
                return new Color32(byte.Parse(splitedValue[0]), byte.Parse(splitedValue[1]), byte.Parse(splitedValue[2]), byte.Parse(splitedValue[3]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    Color32 color32 = item.Value;
                    binaryWriter.Write(color32.r);
                    binaryWriter.Write(color32.g);
                    binaryWriter.Write(color32.b);
                    binaryWriter.Write(color32.a);

                }
            }
        }
    }
}
